package com.mulanweb.api.module.controller.admin;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.mulanweb.api.module.controller.BaseController;
import com.mulanweb.api.module.model.City;
import com.mulanweb.db.common.SQLGenerateHelper;
import com.mulanweb.validator.annotation.ValidateFile;
import com.mulanweb.validator.annotation.ValidateForm;

import java.util.Date;

/**
 * Created by Administrator on 2016/8/19.
 */
@ValidateFile
public class CityController extends BaseController {

    @ValidateForm
    public void save() {
        String cityName = getPara("cityName");
        City tt = City.dao.findFirst("select * from city where name_cn = ?", cityName);
        if (tt != null) {
            returnJson(0, "此地点已存在:"+tt.getId());
        } else {
            City city = new City();
            city.setNameCn(cityName);
            city.setSequence(getParaToInt("sequence"));
            city.setCreateTime(new Date());
            if (city.save())
                returnSuccessJson();
            else
                returnFailureJson();
        }
    }

    @ValidateForm
    public void delete(){
        Long cityId = getParaToLong("cityId");
        City city = City.dao.findById(cityId);
        if(StrKit.notNull(city)){
            if(city.getDeleteTime() == null){
                city.setDeleteTime(new Date());
            }else{
                city.setDeleteTime(null);
            }
            if(city.update()){
                returnSuccessJson();
            }else{
                returnFailureJson();
            }
        }
    }

    @ValidateForm
    public void update(){
        Long cityId = getParaToLong("cityId");
        String name = getPara("cityName");
        Integer sequence = getParaToInt("sequence");
        City city = City.dao.findById(cityId);
        if(StrKit.notNull(city)){
            city.setNameCn(name);
            city.setSequence(sequence);
            if(city.update()){
                returnSuccessJson();
            }else {
                returnFailureJson();
            }
        }else{
            returnJson(0,"城市不存在");
        }
    }

    @ValidateForm
    public void reset(){
        Long cityId = getParaToLong("cityId");
        City city = City.dao.findById(cityId);
        if(city!=null){
            city.setDeleteTime(null);
            city.update();
            returnSuccessJson();
        }else{
            returnFailureJson();
        }
    }
}
